package com.breeze.leetcode.easy;

import java.util.Arrays;

/**
 * @url <a href="https://leetcode.cn/problems/number-of-even-and-odd-bits">2595. 奇偶位数</a>
 * @author yangzh
 * @date 2025/2/20
 */
public class T2595 {
    /**
     * 1    1       [1, 0]      2^0
     * 2    10      [0, 1]      2^1
     * 3    11      [1, 1]      2^1+2^0
     * 4    100     [1, 0]      2^2  幂次是偶数就是[1, 0],幂次是奇数就是[0, 1]
     * 5    101     [2, 0]      2^2+2^0
     * 6    110     [1, 1]      2^2+2^1
     * 7    111     [2, 1]      2^2+2^1+2^0 [1,0]+[0,1]+[1,0]
     * 8    1000    [0, 1]      2^3  [0,1]
     * 9    1001    [1, 1]      2^3+2^0 [1,1]
     * 10   1010    [0, 2]      2^3+2^1
     * 1000 001111101000
     */
    public static void main(String[] args) {
        T2595 solution = new T2595();
        int[] res = solution.solution(1000);
        System.out.println(Arrays.toString(res));
    }

    public int[] evenOddBit(int n) {
        int[] res = {0, 0};
        int[] binary = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
        while (n > 0) {
            for (int i = binary.length - 1; i >= 0; i--) {
                if (n >= binary[i]) {
                    res[i % 2]++;
                    n -= binary[i];
                }
            }
        }

        return res;
    }

    /**
     * 官方题解
     */
    public int[] solution(int n) {
        int[] res = new int[2];
        int i = 0;
        while (n > 0) {
            res[i] += n & 1;
            n >>= 1;
            i ^= 1;
        }
        return res;
    }

}
